﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DataAccess.Classes;

namespace Webmau.Admin
{
    public partial class PhongTroThem : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
                PopulateControls();
        }

        private void PopulateControls()
        {
            drDiaDiem.DataValueField = "ID";
            drDiaDiem.DataTextField = "TenDiaDiem";
            drDiaDiem.DataSource = DiaDiem1.LayDanhSach();
            drDiaDiem.DataBind();
        }
        protected void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                PhongTro1 data = new PhongTro1();
                data.ID = Convert.ToInt32(drDiaDiem.SelectedValue.ToString());
                data.Ten = txtTen.Text;
                data.ThongTin = txtThongTin.Text;
                data.GiaTien = Convert.ToDecimal(txtGiaPhong.Text);
                data.HinhDaiDien = FileUpload1.FileName.ToString();
                data.HinhChinh = FileUpload1.FileName.ToString();
                if (cbTinhTrang.Checked == true)
                    data.TinhTrang = Convert.ToBoolean("True".ToString());
                if (cbTinhTrang.Checked == false)
                    data.TinhTrang = Convert.ToBoolean("False".ToString());
                data.IDNguoiDung = Session["id"].ToString();
                PhongTro1.Them(data);
                lbThongBao.Text = "Thêm thành công!";
            }
            catch
            {
                lbThongBao.Text = "Thêm thất bại!";
            }
        }
        bool CheckFileType(string fileName)
        {
            string ext = Path.GetExtension(fileName);
            switch (ext.ToLower())
            {
                case ".gif":
                    return true;
                case ".png":
                    return true;
                case ".jpg":
                    return true;
                case ".jpeg":
                    return true;
                case ".bmp":
                    return true;
                default:
                    return false;
            }
        }
        protected void FileUpload1_Init(object sender, EventArgs e)
        {
            if (CheckFileType(FileUpload1.FileName) == true)
            {
                String filePath = "~/HinhPhongTro/" + FileUpload1.FileName;
                FileUpload1.SaveAs(MapPath(filePath));
            }
        }
    }
}